package com.hifar.test.exporttask.service.impl;

import com.hifar.test.exporttask.pojo.SysSyncExportTask;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.test.exporttask.mapper.SysSyncExportTaskMapper;
import com.hifar.test.exporttask.service.ISysSyncExportTaskService;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import com.hifar.sys.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.hifar.plat.officUtil.ExcelTemplateUtil;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.*;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import org.apache.commons.lang3.StringUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.fw.utils.id.IdUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
/**
 * <p>
 * 双网同步-导出-任务表 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-26 14:17:47
 */
@Service
public class SysSyncExportTaskServiceImpl extends ServiceImpl<SysSyncExportTaskMapper, SysSyncExportTask> implements ISysSyncExportTaskService{

	 
		@Override
		public BusiResponse exportExcel(Map<String, Object> pageInfo) {
		    BusiResponse busiResponse = new BusiResponse();
		    String ids = (String)pageInfo.get("ids");

		    String operationType = (String)pageInfo.get("operationType");

		    if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))){
		        ExcelTemplateUtil.exportExcelTemplate(busiResponse, SysSyncExportTask.class, "导入模板", "双网同步-导出-任务表导入数据");
		        return busiResponse;
		    }else {
		        QueryWrapper<SysSyncExportTask> postParam = RequestConver.getPostParam(pageInfo);
		        postParam.ne("del_flag", 1);
		        postParam.orderByDesc("create_time");
		        if (StringUtils.isNotBlank(ids)) {
		            postParam.in("id", ids);
		        }
		        List<SysSyncExportTask> dataList = this.list(postParam);
		         if(dataList!=null && dataList.size()>0) {
		        	  ExcelTemplateUtil.exportExcel(busiResponse, dataList, SysSyncExportTask.class, "双网同步-导出-任务表数据", "双网同步-导出-任务表导出");
		        }else {
		        	ExcelTemplateUtil.exportExcelTemplate(busiResponse, SysSyncExportTask.class, "双网同步-导出-任务表数据", "双网同步-导出-任务表导出");
		        }
		        return busiResponse;
		    }

		}

		@Override
		@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
		public BusiResponse importExcel(MultipartFile file) throws Exception {
		    if (file != null && !file.isEmpty()) {
		        List<SysSyncExportTask> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), SysSyncExportTask.class);
		        List<SysSyncExportTask> importExcelList=new ArrayList<SysSyncExportTask>();
		        if (importExcel != null && importExcel.size() > 0) {
		                for (SysSyncExportTask bean : importExcel) {
		                	bean.setId(IdUtils.uuid32());
		                	//if(!bean.getName().equals("")) {
		                		importExcelList.add(bean);
		                	//}
		                }
		                this.saveBatch(importExcelList);
		             return BusiResponse.ok("导入成功！");
		        }
		    }
		    return BusiResponse.error("导入失败，原因未知！");
		}
	 
}
